package com.aster.common.api.wechat.util.MD5;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * <strong>Title : MD5Util<br>
 * </strong> <strong>Description : 简单的MD5加密</strong><br>
 * <strong>Create on : 2011-12-12<br>
 * </strong>
 * <p>
 * <strong>Copyright (C) 武汉天喻通讯<br>
 * </strong>
 * </p>
 * 
 * @author wangdr wangdr@whty.com.cn<br>
 * @version <strong>海南无线城市-应用 v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 修改日期 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
public class MD5Util {

	/**
	 * @Description 获取MD5字符串
	 *              Add by wangdr at 2011-11-25 11:20
	 * 
	 * @param String plainText 加密前的明文字符串
	 * @return String 加密后的MD5字符串
	 */
	public static String getMd5(String plainText) {
		StringBuffer buf = new StringBuffer("");
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

			int i;
			for (byte element : b) {
				i = element;
				if (i < 0) {
					i += 256;
				}
				if (i < 16) {
					buf.append("0");
				}
				buf.append(Integer.toHexString(i));
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return buf.toString();
	}

	/**
	 * main
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// System.out.println("Md5('F#PubC$Ku'): " + getMd5("F#PubC$Ku"));//32位的加密
		// System.out.println("Md5('tianyu'): " + getMd5("tianyu"));//32位的加密
		// System.out.println("Md5('tianyu'): " + getMd5("tianyu").substring(8,24));//16位的加密
		// System.out.println("Md5('tianyu'): " +
		// getMd5("orderId=12HAK133F008172orderState=2 requestTime=2012.05.30 16:20:0012345678").toUpperCase());//32位的加密
		// System.out.println("Md5('resid=12HAK133F014843&corpid="+Constant.CRH_INTERFACE_CORP_ID+"&userid="+Constant.CRH_INTERFACE_KEY+"'): "
		// +
		// getMd5("resid=12HAK133F014843&corpid="+Constant.CRH_INTERFACE_CORP_ID+"&userid="+Constant.CRH_INTERFACE_KEY));//32位的加密
		// System.out.println("Md5('Sign'): " +
		// getMd5("CRHOrderBean.order_status=1&CRHOrderBean.reqtime=2012.10.09 09:05:27&CRHOrderBean.res_id=12HAK133F018756"+Constant.CRH_SECRET_KEY).toUpperCase());//32位的加密
		// System.out.println("Md5('Sign'): " +
		// getMd5("CRHOrderBean.order_status=4&CRHOrderBean.pay_type=1&CRHOrderBean.reqtime=2012.10.09 15:00:00&CRHOrderBean.res_id=12HAK133F011435"+Constant.CRH_SECRET_KEY).toUpperCase());//32位的加密
		System.out
				.println("Md5('Sign'): "
						+ getMd5(
								"lotteryOrderBean.amount=10&lotteryOrderBean.buyType=2&lotteryOrderBean.createDate=2014-01-13 01:30:58&lotteryOrderBean.orderStatus=1&lotteryOrderBean.orderNo=123456&lotteryOrderBean.paymentDate=&lotteryOrderBean.paymentMethod=&lotteryOrderBean.paymentPhone=&lotteryOrderBean.paymentStatus=&lotteryOrderBean.siteId=460100&lotteryOrderBean.userId=13976241915&reqTime=2014-01-13 01:30:58&synType=add12345678")
								.toUpperCase());// 32位的加密
		System.out
				.println("Md5('Sign'): "
						+ getMd5(
								"lotteryOrderBean.amount=10&lotteryOrderBean.buyType=2&lotteryOrderBean.createDate=2014-01-13 01:30:58&lotteryOrderBean.orderStatus=3&lotteryOrderBean.orderNo=123456&lotteryOrderBean.paymentDate=2014-01-13 01:30:58&lotteryOrderBean.paymentMethod=&lotteryOrderBean.paymentPhone=&lotteryOrderBean.paymentStatus=&lotteryOrderBean.siteId=460100&lotteryOrderBean.userId=13976241915&reqTime=2014-01-13 01:30:58&synType=update12345678")
								.toUpperCase());// 32位的加密
	}
}
